﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace BDH.PRE
{
    using BDH.DAL;
    public partial class FrmSetting : DevExpress.XtraEditors.XtraForm
    {
        private const string STR_FIND = "<Tìm kiếm>";
        private const string STR_WIND = "Windows";

        private const string STR_SETUP = "Cài đặt";
        private const string STR_EXISTS = "Cơ sở dữ liệu đã có!";
        private const string STR_NOCONNECT = "Không kết nối được server!";
        private const string STR_TEMPLATE = "Đã tạo xong dữ liễu mẫu!";
        private const string STR_SET_TEMP = "Cài dữ liệu mẫu không?";
        private const string STR_NOT_FOUND = "Không tìm thấy file cấu hình!";
        private const string STR_SAVE = "Đã lưu cấu hình!";

        /// <summary>
        /// Get string connect for ADO.NET Entity Framework
        /// </summary>
        private string StringConnectEnt
        {
            get
            {
                string sver = cbbServer.Text;
                string user = cbbUser.Text;
                string pass = txtPass.Text;
                string data = cbbDb.Text;
                string str = "";

                if (data == "" || data == STR_FIND) data = DAL.BaseDAL.DbName;
                if (cbbAuthen.Text == STR_WIND) str = String.Format(DAL.BaseDAL.STR_ETRU, sver, data);
                else str = String.Format(DAL.BaseDAL.STR_ESEC, sver, data, user, pass);

                return str;
            }
        }

        /// <summary>
        /// Get string connect
        /// </summary>
        private string StringConnect
        {
            get
            {
                string sver = cbbServer.Text;
                string user = cbbUser.Text;
                string pass = txtPass.Text;
                string data = cbbDb.Text;
                string str = "";

                if (data == "" || data == STR_FIND) data = DAL.BaseDAL.DbName;
                if (cbbAuthen.Text == STR_WIND) str = String.Format(DAL.SQLServer.STR_TRU, sver, data);
                else str = String.Format(BDH.DAL.SQLServer.STR_SEC, sver, data, user, pass);

                return str;
            }
        }

        /// <summary>
        /// Get string connect for setup
        /// </summary>
        public string StringConnectSetup
        {
            get
            {
                string sver = cbbServer.Text;
                string user = cbbUser.Text;
                string pass = txtPass.Text;
                const string data = "master";
                string str = "";

                if (cbbAuthen.Text == STR_WIND) str = String.Format(DAL.SQLServer.STR_TRU, sver, data);
                else str = String.Format(DAL.SQLServer.STR_SEC, sver, data, user, pass);

                return str;
            }
        }

        public FrmSetting()
        {
            InitializeComponent();
        }

        private void FrmSetting_Load(object sender, EventArgs e)
        {
            // Get current information connect string
            var tmp = DAL.BaseDAL.CurrentConnectInfo;
            cbbServer.Text = tmp.Server;
            cbbDb.Text = tmp.Database;
            cbbUser.Text = tmp.User;
            txtPass.Text = tmp.Password;

            // Select windows authentication
            if (tmp.User == "") cbbAuthen.SelectedIndex = 1;

            // Check database exist & disable button Setup
            var x = new DAL.SQLServer();
            if (x.CheckDbExists(DAL.SQLServer.DbName)) cmdSetup.Enabled = false;
            else cmdSetup.Enabled = true;
        }

        private void cbbServer_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (cbbServer.SelectedItem + "" == STR_FIND)
                {
                    var tbl = DAL.BaseDAL.GetSQLServers();
                    cbbServer.Properties.Items.Remove(STR_FIND);

                    cbbServer.Properties.Items.Clear();
                    foreach (DataRow dtr in tbl.Rows)
                        cbbServer.Properties.Items.Add(dtr["name"]);

                    cbbServer.Properties.Items.Add(STR_FIND);

                    if (cbbServer.Properties.Items.Count > 1) cbbServer.SelectedIndex = 0;
                    else cbbServer.SelectedIndex = -1;
                }
            }
            catch { return; }
        }

        private void cbbAuthen_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbAuthen.SelectedItem + "" == STR_WIND)
            {
                cbbUser.Enabled = false;
                txtPass.Enabled = false;
            }
            else
            {
                cbbUser.Enabled = true;
                txtPass.Enabled = true;
            }
        }

        private void cmdClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cbbDb_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (cbbDb.SelectedItem + "" == STR_FIND)
                {
                    var tbl = (new DAL.SQLServer(StringConnect)).GetDatabases();
                    cbbDb.Properties.Items.Remove(STR_FIND);

                    cbbDb.Properties.Items.Clear();
                    foreach (DataRow dtr in tbl.Rows)
                        cbbDb.Properties.Items.Add(dtr["name"]);

                    cbbDb.Properties.Items.Add(STR_FIND);

                    if (cbbDb.Properties.Items.Count > 1) cbbDb.SelectedIndex = 0;
                    else cbbDb.SelectedIndex = -1;
                }
            }
            catch { return; }
        }

        private void cmdSetup_Click(object sender, EventArgs e)
        {
            var dbName = DAL.SQLServer.DbName;
            var x = new DAL.SQLServer(StringConnectSetup);

            var xFile = DAL.SQLServer.FileX;
            var yFile = DAL.SQLServer.FileY;

            if (x.Open())
            {
                if (!x.CheckDbExists(dbName)) // database not exists
                {
                    if (x.CreateDbName(dbName)) // create database                    
                        if (x.ExecuteFileSQL(dbName, xFile, false))
                        {
                            // Set template data
                            if (MessageBox.Show("Cài đặt dữ liệu mẫu", "Thông Báo", MessageBoxButtons.OKCancel)
                                == DialogResult.OK)
                            {
                                if (x.ExecuteFileSQL(dbName, yFile, false))
                                {
                                    MessageBox.Show("Cài đặt dữ liệu mẫu thành công", "Thông Báo");
                                    FrmMain._setDB = true;
                                }
                            }

                            cmdSetup.Enabled = false;
                            Close(); // close form
                        }
                }
                else
                {
                    MessageBox.Show("Cơ sở dữ liệu đã có", "Thông Báo");
                    cmdSetup.Enabled = false;
                }
            }
            else MessageBox.Show("Không kết nối", "Thông Báo");
        }


    }
}